/**
 * 
 */
package com.wodsy.phoenix.daemon.cms.listener;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.common.json.JSON;
import com.wodsy.phoenix.daemon.cms.task.TaskQueneMap;
import com.wodsy.phoenix.daemon.cms.task.TimerFactory;
import com.wodsy.phoenix.dataobject.cms.model.StatusTaskVo;
import com.wodsy.phoenix.dataobject.cms.vo.IdxAdPicResp;
import com.wodsy.phoenix.dataobject.cms.vo.IdxCustomizePicResp;
import com.wodsy.phoenix.dataobject.cms.vo.IdxHotGoodsRecResp;
import com.wodsy.phoenix.dataobject.cms.vo.IdxNewGoodsRecResp;
import com.wodsy.phoenix.dataobject.cms.vo.IdxShopRecResp;
import com.wodsy.phoenix.dataservice.cms.service.CmsHomePageBannerService;
import com.wodsy.phoenix.dataservice.cms.service.CmsHomePageCustomizePicService;
import com.wodsy.phoenix.dataservice.cms.service.CmsHomePageHotGoodsService;
import com.wodsy.phoenix.dataservice.cms.service.CmsHomePageNewGoodsService;
import com.wodsy.phoenix.dataservice.cms.service.CmsHomePageShopRecService;
import com.wodsy.phoenix.dataservice.cms.service.CmsHomePageStatusService;
import com.wodsy.phoenix.scaffold.Scaffold_web_index;



/**
 * @author bryan
 *
 */
public class HomePageStatusChangeHandler implements MessageListener{
	@Autowired
	private CmsHomePageBannerService cmsHomePageBannerService;
	@Autowired
	private CmsHomePageCustomizePicService cmsHomePageCustomizePicService;
	@Autowired
	private CmsHomePageHotGoodsService cmsHomePageHotGoodsService;
	@Autowired
	private CmsHomePageNewGoodsService cmsHomePageNewGoodsService;
	@Autowired
	private CmsHomePageShopRecService cmsHomePageShopRecService;
	
	@Autowired
	private CmsHomePageStatusService cmsHomePageStatusService;
	//@Autowired
	//private SpringContextUtil springContextUtil;
	
    private static Logger logger = Logger.getLogger(HomePageStatusChangeHandler.class);  
    
		@Override
		public void onMessage(Message objMsg) {
			logger.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
			TextMessage msg = (TextMessage) objMsg;
			try {
				String json = msg.getText();
				net.sf.json.JSONObject js = new net.sf.json.JSONObject();
				
				StatusTaskVo stv = (StatusTaskVo) net.sf.json.JSONObject.toBean(js.fromObject(json), StatusTaskVo.class);
				dealWithStatusChange(stv);
				logger.info("receive message : " + json);
			} catch (JMSException e) {
				e.printStackTrace();
				logger.error(
						"Error: receive message from topic failure: "
								+ e.getMessage(), e);
			}
		}

		private void dealWithStatusChange(final StatusTaskVo stv){
			TimerTask task = new TimerTask() {
            	@Override
				public void run() {
            		switch (stv.getObjType()) {
        				case 0://banner
    	                	cmsHomePageStatusService.updateBannerRecommendedStatus(stv.getId(), stv.getStatus());
        					break;
        				case 1://
    	                	cmsHomePageStatusService.updateNewGoodsRecommendedStatus(stv.getId(), stv.getStatus());
                    		//System.out.println("执行时间"+DateUtils.getNowTime()+"  执行数据 :"+stv.getStatus());
    	                	break;
        				case 2:
        					cmsHomePageStatusService.updateHotGoodsRecommendedStatus(stv.getId(), stv.getStatus());
                    		break;
        				case 3:
        					cmsHomePageStatusService.updateCustomizeRecommendedStatus(stv.getId(), stv.getStatus());
                    		break;
        				case 4:
        					cmsHomePageStatusService.updateShopRecommendedStatus(stv.getId(), stv.getStatus());
                    		break;
        				default:
        				break;
        			}
            		System.out.println("执行完毕，准备生成首页");
            		generateHTML();
				}
			};
			
			String keyId = stv.getObjType()+""+stv.getStatus()+""+stv.getId();
			if(TaskQueneMap.taskMap.keySet().contains(keyId)){
        		TimerTask tt = (TimerTask)TaskQueneMap.taskMap.get(keyId);
        		//存在的值，取消
        	    Boolean bl = tt.cancel();
        	    TaskQueneMap.taskMap.remove(keyId);
        	}
        	Timer tm = TimerFactory.getInstance();
        	long delayDis = stv.getTime()-new Date().getTime();
        	tm.schedule(task, delayDis);
        	TaskQueneMap.taskMap.put(keyId, task);
		}
		
		private void generateHTML(){
			HashMap<String, String> hMap = new HashMap<>();
	    	
	    	try {
	    		List<IdxAdPicResp> bannertblist = cmsHomePageBannerService.getOnlinePicBanner();
	        	List<IdxNewGoodsRecResp> newtblist = cmsHomePageNewGoodsService.getOnlineNewGoodsRec();
	        	List<IdxHotGoodsRecResp> hottblist = cmsHomePageHotGoodsService.getOnlineHotGoodsRec();
	        	IdxCustomizePicResp pictb = cmsHomePageCustomizePicService.getOnlineCustomizePicRec();
	        	IdxShopRecResp shoptb = cmsHomePageShopRecService.getOnlineShopReccommendedRec();
	        	
	        	hMap.put("focusdate", JSON.json(bannertblist));
	        	hMap.put("newGoods",JSON.json(newtblist));
	        	hMap.put("hotGoods",JSON.json(hottblist));
	        	hMap.put("customizePic",JSON.json(pictb));
	        	hMap.put("shopRec",JSON.json(shoptb));
	        	
	        	Scaffold_web_index.generateSourceFileFromTemplate("index4.ftl", "index3", hMap);
	    	} catch (Exception e) {
				logger.error(e.getMessage());
			}
		}

}
